package com.example.mardan.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mardan.entity.CatalogEntity;
import com.example.mardan.views.BooksActivity;
import com.example.read.R;

import java.util.List;

public class CatalogAdapter extends RecyclerView.Adapter<CatalogAdapter.ViewHolder> {
    private List<CatalogEntity.BookList> bookTypeList ;
    private Context context ;


    public CatalogAdapter(List<CatalogEntity.BookList> bookTypeList, Context context) {
        this.bookTypeList = bookTypeList;
        this.context = context;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_main,null);
        return new ViewHolder(view) ;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CatalogEntity.BookList bookType = bookTypeList.get(position);
        holder.textView.setText(bookType.getCatalog());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context , BooksActivity.class);
                intent.putExtra("catalog_id",bookType.getId());
                intent.putExtra("title",bookType.getCatalog());
                context.startActivity(intent);

            }
        });
    }

    @Override
    public int getItemCount() {
        return bookTypeList.size();
    }


    public void updateAdapter(List<CatalogEntity.BookList> list){

            bookTypeList.clear();
            this.bookTypeList = list;
            notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView textView ;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.item_main_type);
        }
    }
}
